﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Telerik.WebControls;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;

public partial class Theme_DL_DealClassTask : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string taskid = "0";
    protected string courseid = "0";
    VOTOClassTasks votask = new VOTOClassTasks();
    VOTOUserClassTask vomytask = new VOTOUserClassTask();
    TOClassTasks objtask = new TOClassTasks();
    TOUserClassTask objmytask = new TOUserClassTask();
    Course opCourse = new Course();
    VOTOCourse vocourse = new VOTOCourse();
    TOClass objclass = new TOClass();
    VOTOClass voclass = new VOTOClass();
    VOTOAccessory voaccessory = new VOTOAccessory();
    TOAccessory objaccessory = new TOAccessory();
    PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    protected string UserTask_ID = "0";
    protected string classID = "0";
    protected string courseListID = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];

        if (Request.QueryString["UserTask_ID"] != null)
        {
            UserTask_ID = Request.QueryString["UserTask_ID"].ToString();
            ViewState["UserTask_ID"] = UserTask_ID;
            //vocourse = opCourse.LoadVOTCourse(int.Parse(courseid));
            //lbl_course.Text = vocourse.CourseName;
        }

        if (Request.QueryString["CourseListID"] != null)
        {
            courseListID = Request.QueryString["CourseListID"].ToString();
            ViewState["CourseListID"] = courseListID;
        }

        if (!IsPostBack)
        {
            if (Request.QueryString["CourseID"] != null)
            {
                courseid = Request.QueryString["CourseID"].ToString();
                ViewState["CourseID"] = courseid;
                vocourse = opCourse.LoadVOTCourse(int.Parse(courseid));
                lbl_course.Text = vocourse.CourseName;
            }
            if (Request.QueryString["TaskID"] != null)
            {
                taskid = Request.QueryString["TaskID"].ToString();
                ViewState["TaskID"] = taskid;
                votask = objtask.Load(int.Parse(taskid));
                lbl_TaskTitle.Text = votask.TasksTitle;
                lbl_TaskTitleInfo.Text = votask.TasksTitle;
                lbl_TaskContent.Text = votask.TasksContent;
                if (votask.Accessory != null)
                {
                    voaccessory = objaccessory.Load(votask.Accessory);
                    lbl_TaskAccessory.Text = "<a href=\"" + voaccessory.Url + "\" target=\"_blank\">" + voaccessory.Title + "</a>";
                }
                else
                {
                    lbl_TaskAccessory.Text = "无";
                }

                lbl_endtime.Text = votask.EndDate.ToString().Replace(" 0:00:00", ""); ;
                BinderPage(lbl_endtime.Text);
            }


            if (Request.QueryString["ClassID"] != null)
            {
                classID = Request.QueryString["ClassID"].ToString();
                ViewState["ClassID"] = classID;
                voclass = objclass.Load(int.Parse(classID));
                lbl_ClassName.Text = voclass.Name;
                lbl_Class.Text = voclass.Name;
            }

        }
        
    }

    private void BinderPage(string endtime)
    {
        string search = " Task_Id=" + ViewState["TaskID"].ToString() + " and Creator=" + login.UserID.ToString();
        DataTable dt = new DataTable();
        dt = objmytask.Query(search);
        if (dt.Rows.Count > 0)
        {
            UserTask_ID = dt.Rows[0]["UserTask_ID"].ToString();
            txt_MyTaskTitle.Text = dt.Rows[0]["Title"].ToString();
            txt_MyTaskContent.Text = dt.Rows[0]["Content"].ToString();
            if(dt.Rows[0]["Accessory"]!=null)
            {
                if (dt.Rows[0]["Accessory"].ToString().Length > 0)
                {
                    voaccessory = objaccessory.Load(int.Parse(dt.Rows[0]["Accessory"].ToString()));
                    lbl_MyTaskAccessory.Text = "<a href=\"" + voaccessory.Url + "\" target=\"_blank\">" + voaccessory.Title + "</a>";
                    lbl_MyAccessoryid.Text = voaccessory.Accessory_ID.ToString();
                }

            }
        }
        if (DateTime.Today > DateTime.Parse(endtime))
        {
            txt_MyTaskTitle.Enabled = false;
            txt_MyTaskContent.Enabled = false;
            btnSave.Enabled = false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (login != null)
        {
            if (UserTask_ID != "0")
                vomytask = objmytask.Load(int.Parse(UserTask_ID));
            if (ViewState["TaskID"] != null)
            {
                vomytask.Task_Id = int.Parse(ViewState["TaskID"].ToString());
                vomytask.Title = txt_MyTaskTitle.Text.Trim();
                vomytask.Content = txt_MyTaskContent.Text.Trim();
                vomytask.CourseID = int.Parse(ViewState["CourseID"].ToString());
                if (lbl_MyAccessoryid.Text.Length > 0)
                    vomytask.Accessory = int.Parse(lbl_MyAccessoryid.Text.Trim());
                vomytask.Creator = login.UserID;
                vomytask.CreateDate = DateTime.Now;
            }
            bool b = false;
            if (UserTask_ID != "0")
                b = objmytask.Update(vomytask);
            else
                b = objmytask.Add(vomytask);

            if (b)
            {
                string url = "MyClassTasks.aspx?ClassID=" + ViewState["ClassID"].ToString();
                Script.RedirectURL(url);
            }
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //btn_Back.Enabled = false;
        //Session["pwd"] = txt_UserPwd.Text;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Url = "";
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/Accessory/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);

            VOTOAccessory vo = new VOTOAccessory();
            TOAccessory obj = new TOAccessory();
            vo.Url = Url;
            if (obj.Count(" Title='" + txt_MyTaskTitle.Text.Trim() + "' and Url='" + Url + "' ") == 0)
            {
                int num = obj.Count(" Title like '" + txt_MyTaskTitle.Text.Trim() + "%' ");
                if (num > 0)
                {
                    vo.Title = txt_MyTaskTitle.Text.Trim() + num.ToString();
                }
                else
                {
                    vo.Title = txt_MyTaskTitle.Text.Trim();
                }
                vo.Createtime = DateTime.Now;
                PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
                vo.Creator = bpage.GetCreatedByInfo().UserID;
                int Accessory_ID = System.Int32.MinValue;
                if (obj.Add(vo, ref Accessory_ID))
                {
                    lbl_MyTaskAccessory.Text = txt_MyTaskTitle.Text.Trim();
                    lbl_MyAccessoryid.Text = Accessory_ID.ToString();
                    //ListItem li = new ListItem();
                    //li.Text = vo.Title;
                    //li.Value = Accessory_ID.ToString();
                    //ddl_Accessory.Items.Insert(0, li);
                    //ddl_Accessory.ClearSelection();
                    //ddl_Accessory.SelectedValue = Accessory_ID.ToString();
                    //BinderAccessory();
                    //ddl_Accessory.SelectedValue = vo.Accessory_ID.ToString();
                }


            }
            //img_UserPic.ImageUrl = Url;
            //txt_UserPwd.Text = Session["pwd"].ToString();
            btn_Back.Enabled = true;
        }
    }

    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "rar|zip|doc|xls|ppt|txt";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            lbl_FileUpload_msg_FileType.Text = GetLanageValueByKey("AddCourseInfo_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + allowExtensions;
            return false;
        }
        return true;
    }

}
